<?php
namespace Admin\Model;
use Think\Model;
/**
 * 个人日程管理模型
 * 
 */

class LogModel extends Model{
	const AUDIT_NEED = 0;/* 待审核 */
	const AUDIT_OK = 1;
	const AUDIT_ERR = 2; /* 审核失败 */

	protected $m_types = array('0'=>'日报','1' =>'周报','2' => '月报');

	public function indexs(){
        $user = M('user');
        $uinfo = UserModel::loadFromSession();
        $id = $uinfo['user_id'];
        $res = $user ->where('user_id='.$id)->find();
        return $res;
    }
	
	public function getTypes(){
		$types = array();
		foreach( $this->m_types as $kv=>$vt ){
			$item = array('k'=>$kv,'v'=>$vt);
			$types[] = $item;
		}
		return $types;
	}

	//添加日志接口
	public function add_log( $title, $content ,$type )
	{
		$uinfo = UserModel::loadFromSession();

		$r = array('d'=>NULL,'i'=>'','r'=>0);
		if( !$uinfo )
			$r['i'] = '加载用户信息失败！';
		else if( !$title || !$content || false === $type )
			$r['i'] =  L('表单信息获取失败！');
		else{
			$d = array();
			$d['title'] = $title;
			$d['type'] = $type;
			$d['content'] = $content;
			$d['create_time'] = date('Y-m-d H:i:s');
			$d['update_time'] = $d['create_time'];
			$d['creator_id'] = $uinfo['user_id'];
			$d['creator_sector'] =  $uinfo['sector_id'];
			$d['creator_role'] = $uinfo['role_id'];		
			$d['is_deleted'] = 0;		 
			$addID = $this->add( $d );
			if( $addID ){
				$r['r'] = $addID ;
				$r['i'] = 'OK';
				$r['d'] = $this->where( 'log_id='.$addID )->find();
			}else{
				$r['i'] = '日志添加失败！';
			}
		}
		return $r;
	}

	//删除日志
	public function delete_log( $log_id ){
		$r = array('r'=>0,'i'=>'server error','d'=>NULL );
		if( $log_id )
			$r['i'] = '参数错误';

		$wheres = array();
		$wheres['log_id'] = array('eq', $log_id);
		$wheres['is_deleted'] = array('neq', 1);
		$find = $this->where( $wheres )->find();
		if( $find ){
			$d = array();
			$d['is_deleted'] = 1;
			$delID = $this->where( $wheres )->save( $d );
			if( $delID  ){
				$r['r'] = 1;
				$r['i'] = 'OK';
			}else{
				$r['i'] = '日志删除失败！';
			}
		}
		else
			$r['i'] = '数据不存在';
 		return $r;
	}

	//更新日志
	public function update_log($editArr){
		$r = array('r'=>0,'i'=>'server error','d'=>NULL );
		if( $editArr['log_id'] && $editArr['title'] && $editArr['content'] ) {
			$dlog = null;
			$wheres = array();
			$wheres['log_id'] = array('eq', $editArr['log_id'] );
			$wheres['is_deleted'] = array('neq', 1);
			$dlog = $this->where( $wheres )->find();
			if( $dlog ){
				if( $dlog['audit_res'] != self::AUDIT_NEED && $dlog['audit_res'] !=self::AUDIT_ERR ){
					$r['i'] = '日志已经审核，不能修改';
				}else{
					$d = array();
					$d['update_time'] = date('Y-m-d H:i:s');
					$d['title'] = $editArr['title'];
					$d['content'] = $editArr['content'];
					$d['type'] = $editArr['type'];
					if( $this->where( $wheres )->save( $d )){
						$r['r'] = 1;
						$r['d'] = $this->where( $wheres )->find();
						$r['i'] = 'OK';
					}
					else{
						$r['i'] = '日志更新失败！';
					}
 				}
			}
			else
				$r['i'] = '数据不存在';
		} else{
			$r['i'] = '参数错误';
		}
		return $r;
	}

	//审核日志通过
	public function audit_logok( $log_id,$audit_msg){
		
		$uinfo = UserModel::loadFromSession();
		$r = array('d'=>NULL,'i'=>'','r'=>0);
		if( !$uinfo )
			$r['i'] = '加载用户信息失败';
		else{
			$wheres = array();
			$wheres['log_id'] = array('eq', $log_id );
			$wheres['is_deleted'] = array('neq', 1);
			$d = array();
			$d['audit_id'] = $uinfo['user_id'];
			$d['audit_sector'] = $uinfo['sector_id'];
			$d['audit_role'] = $uinfo['role_id'];
			$d['audit_res'] = 1;
			$d['audit_msg'] = $audit_msg;
			$d['audit_time'] = date('Y-m-d H:i:s');
			$addre =  $this->where( $wheres )->save( $d );
			if($addre){
				$r['r'] = 1;
				$r['d'] = $this->where( $wheres )->find();
				$r['i'] = 'OK';
			}
			else{
				$r['i'] = '审核失败';
			}

		}
		return $r;
	}


	//审核日志不通过
	public function audit_logno( $log_id,$audit_msg){
		
		$uinfo = UserModel::loadFromSession();
		$r = array('d'=>NULL,'i'=>'','r'=>0);
		if( !$uinfo )
			$r['i'] = '加载用户信息失败';
		else{
			$wheres = array();
			$wheres['log_id'] = array('eq', $log_id );
			$wheres['is_deleted'] = array('neq', 1);
			$d = array();
			$d['audit_id'] = $uinfo['user_id'];
			$d['audit_sector'] = $uinfo['sector_id'];
			$d['audit_role'] = $uinfo['role_id'];
			$d['audit_res'] = 2;
			$d['audit_msg'] = $audit_msg;
			$d['audit_time'] = date('Y-m-d H:i:s');
			$addre =  $this->where( $wheres )->save( $d );
			if($addre){
				$r['r'] = 1;
				$r['d'] = $this->where( $wheres )->find();
				$r['i'] = 'OK';
			}
			else{
				$r['i'] = '审核失败';
			}
		}
		return $r;
	}

	public function str_cut($begin,$end,$str){
          $b = mb_strpos($str,$begin) + mb_strlen($begin);
          $e = mb_strpos($str,$end) - $b;

          return mb_substr($str,$b,$e);
        }

}
?>